﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BF.IBLL.IXZCF_WS_AJSLJLBLLLibrary;
using BF.Model;
using Base.Core.UnityFactory;
using BF.Infrastructure;
using System.Web.UI.HtmlControls;

namespace BF.Web.pages.XZCF_WS_AJSLJLPage
{
    public partial class List : BasePage
    {
        #region 实例
        private readonly IXZCF_WS_AJSLJLBLL ibll = IOCFactory.GetIOCResolve<IXZCF_WS_AJSLJLBLL>();
        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindCotrol();
                BindList();
            }
        }

        private void BindList()
        {

            System.Linq.Expressions.Expression<Func<XZCF_WS_AJSLJL, bool>> FuncWhere;
            //if (User.IsInRole("科长"))
            //{
            //    FuncWhere = P => P.bDel != true && P.iState == 1;
            //}
            //else if (User.IsInRole("分管局长"))
            //{
            //    FuncWhere = P => P.bDel != true && P.iState == 2;
            //}
            //else if (User.IsInRole("法许可审核"))
            //{
            //    FuncWhere = P => P.bDel != true && P.iState == 3;
            //}
            //else if (User.IsInRole("超级管理员"))
            //{
            //    FuncWhere = P => P.bDel != true;
            //}
            //else
            //{
            //    FuncWhere = P => P.bDel != true;
            //}
            FuncWhere = P => P.bDel != true;

            List<XZCF_WS_AJSLJL> list = ibll.GetList(FuncWhere, P => new { P.gKey }, new PageModel(AspNetPager1.CurrentPageIndex), false);
            AspNetPager1.RecordCount = ibll.GetRowCount(FuncWhere);
            Repeater1.DataSource = list.Select(P => new
            {
                gKey = P.gKey,
                sAJLY = P.sAJLY,
                sAFDW = P.sAFDW,
                dSLSJ = P.dSLSJ,
                sAQZY = P.sAQZY,
                sJBRQM = P.sJBRQM,
                dJBRQ = P.dJBRQ,
                iState = GetState(P.iState),
            });
            Repeater1.DataBind();
        }

        protected string GetState(int? state)
        {
            switch (state)
            {
                case 1: return "科长审核";
                case 2: return "分管局长审核";
                case 3: return "法许可审核";
                default: return "未审核";
            }
        }

        private void BindCotrol()
        {
            //InitDLL(ddlScope,"WebNewClass");
        }

        //带请选择的 下拉列表
        private void InitDLL(DropDownList ddl, string strCode)
        {
            List<dic_Dict> modelList = GetDictList(strCode);
            ddl.AppendDataBoundItems = true;
            ddl.Items.Add(new ListItem("--请选择--", ""));
            for (int i = 0; i < modelList.Count; i++)
            {
                ddl.Items.Add(new ListItem(modelList[i].sName.ToString(), modelList[i].sCode.ToString()));
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            bool flag = false;
            XZCF_WS_AJSLJL model = new XZCF_WS_AJSLJL();
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)Repeater1.Items[i].FindControl("chksuserid");
                if (chk != null && chk.Checked)
                {
                    if (chk.Value != "")
                    {
                        Guid gkey = new Guid(chk.Value);
                        model = ibll.GetModel(P => P.gKey == gkey);
                        model.bDel = true;
                    }
                    if (ibll.Update())
                    {
                        flag = true;
                    }
                }
            }
            if (flag)
            {
                Messages("删除成功！");
            }
            else
            {
                Messages("删除失败！");
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindList();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            BindList();
        }

        protected void LinkButton1_Click(object sender, EventArgs e)
        {
            bool flag = false;
            XZCF_WS_AJSLJL model = new XZCF_WS_AJSLJL();
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)Repeater1.Items[i].FindControl("chksuserid");
                if (chk != null && chk.Checked)
                {
                    if (chk.Value != "")
                    {
                        Guid gkey = new Guid(chk.Value);
                        model = ibll.GetModel(P => P.gKey == gkey);
                        model.iState = 1;
                    }
                    if (ibll.Update())
                    {
                        flag = true;
                    }
                }
            }
            if (flag)
            {
                Messages("审核成功！");
            }
            else
            {
                Messages("审核失败！");
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            bool flag = false;
            XZCF_WS_AJSLJL model = new XZCF_WS_AJSLJL();
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)Repeater1.Items[i].FindControl("chksuserid");
                if (chk != null && chk.Checked)
                {
                    if (chk.Value != "")
                    {
                        Guid gkey = new Guid(chk.Value);
                        model = ibll.GetModel(P => P.gKey == gkey);
                        model.iState = 2;
                    }
                    if (ibll.Update())
                    {
                        flag = true;
                    }
                }
            }
            if (flag)
            {
                Messages("审核成功！");
            }
            else
            {
                Messages("审核失败！");
            }
            AspNetPager1.CurrentPageIndex = 1;
        }

        protected void LinkButton3_Click(object sender, EventArgs e)
        {
            bool flag = false;
            XZCF_WS_AJSLJL model = new XZCF_WS_AJSLJL();
            for (int i = 0; i < Repeater1.Items.Count; i++)
            {
                HtmlInputCheckBox chk = (HtmlInputCheckBox)Repeater1.Items[i].FindControl("chksuserid");
                if (chk != null && chk.Checked)
                {
                    if (chk.Value != "")
                    {
                        Guid gkey = new Guid(chk.Value);
                        model = ibll.GetModel(P => P.gKey == gkey);
                        model.iState = 3;
                    }
                    if (ibll.Update())
                    {
                        flag = true;
                    }
                }
            }
            if (flag)
            {
                Messages("审核成功！");
            }
            else
            {
                Messages("审核失败！");
            }
            AspNetPager1.CurrentPageIndex = 1;
        }


    }
}
